/**
 * 
 */
package ua.lviv.franko.solvers.old.twoDimention;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jzy3d.maths.Range;

import ua.lviv.franko.integration.ConstantFunction2D;
import ua.lviv.franko.integration.Gauss2D;
import ua.lviv.franko.integration.IFunction2D;
import ua.lviv.franko.matrix.RowType;
import ua.lviv.franko.matrix.hash.lu.HashMatrixLUStyle;
import ua.lviv.franko.matrix.hash.lu.LUFactorization;
import ua.lviv.franko.mlsFunctions.oneDimension.weightFunctions.CubicSpline;
import ua.lviv.franko.mlsFunctions.twoDimension.BaseFunction;
import ua.lviv.franko.mlsFunctions.twoDimension.Particle;
import ua.lviv.franko.mlsFunctions.twoDimension.weightFunctions.IWeightFunction2D;
import ua.lviv.franko.mlsFunctions.twoDimension.weightFunctions.SquareSupport;
import ua.lviv.franko.solvers.twoDimention.EquationParameters;
import ua.lviv.franko.triangulation.SaveStructures.Node;
import ua.lviv.franko.triangulation.SaveStructures.NodePair;
import ua.lviv.franko.triangulation.SaveStructures.TriangleL;
import ua.lviv.franko.visualisation.EFGResult2D;

/**
 * @author marko
 * 
 */
public class Solver2 {
	// general data
	protected int						quadraturePointsNumber;
	protected Gauss2D					gau;
	protected EquationParameters		parameters;
	protected HashMatrixLUStyle				mainMatrix;
	protected double[]					b;

	// data for background integration;
	protected ArrayList<TriangleL>		triangleList;
	protected ArrayList<Node>			nodeList;
	protected ArrayList<NodePair>		edges;
	// data for base functions
	protected ArrayList<Particle>		particles;
	protected ArrayList<BaseFunction>	baseFunctions;

	public Solver2(int quadraturePointsNumber, ArrayList<Particle> particles, EquationParameters parameters, String folderDir, int PolinomialDegree, IWeightFunction2D weight) {
		// initialization
		this.quadraturePointsNumber = quadraturePointsNumber;
		this.gau = new Gauss2D(quadraturePointsNumber);
		this.particles = particles;
		this.parameters = parameters;

		// creation of base functions;
		this.createBaseFunctions(PolinomialDegree, weight);
		// continuation of initialization
		this.readData(folderDir);
	}

	public EFGResult2D calculate() {
		for (int i = 0; i < b.length; i++) {
			for (int j = 0; j < b.length; j++) {
				System.out.print(mainMatrix.getValue(i, j) + "\t");
				// if(Math.abs(mainMatrix.getValue(i, j) -
				// mainMatrix.getValue(j, i))>0.00000001){
				// System.err.print(i+"\t"+ j+"\t");
				// }

			}
			System.out.println();
		}
		for (int j = 0; j < b.length; j++) {
			System.out.println(b[j]);
		}
		for (int i = 0; i < b.length; i++) {
			BaseFunction bFunc = baseFunctions.get(i);
			// if
			// ((Math.abs(Math.abs(bFunc.getXi().getXi())-0.5)<0.00000001)||(Math.abs(Math.abs(bFunc.getXi().getYi())-0.5)<0.00000001))
			// {
			if ((Math.abs(Math.abs(bFunc.getXi().getXi()) - 0.5) < 0.0000001)) {
				mainMatrix.setValue(i, i, 100000.0);
				b[i] = 0.0;
			}
			// if
			// ((!(Math.abs(Math.abs(bFunc.getXi().getXi())-0.5)<0.00000001))&&(Math.abs(Math.abs(bFunc.getXi().getYi())-0.5)<0.00000001))
			// {
			// mainMatrix.setValue(i, i, 100000.0);
			// b[i] = 0.0;
			// }
		}
		double[][] bb = new double[b.length][2];
		for (int i = 0; i < bb.length; i++) {
			for (int j = 0; j < bb[0].length; j++) {
				bb[i][j] = b[i];
			}
		}

		LUFactorization lu = new LUFactorization(mainMatrix);
		double[][] rr = lu.calculate(bb);
		double[] r = lu.calculate(b);
		for (int j = 0; j < b.length; j++) {
			System.out.println(rr[j][0] + "\t" + r[j] + "\t" + (rr[j][0] - r[j]));
		}

		return new EFGResult2D(r, baseFunctions);
	}

	public EFGResult2D calculateLagrange() {
		int N = 0;
		ArrayList<BaseFunction> bounds = new ArrayList<BaseFunction>();
		for (Iterator<BaseFunction> iterator = baseFunctions.iterator(); iterator.hasNext();) {
			BaseFunction bFunc = iterator.next();
			if ((Math.abs(Math.abs(bFunc.getXi().getXi()) - 0.5) < 0.0001) || (Math.abs(Math.abs(bFunc.getXi().getYi()) - 0.5) < 0.0001)) {
				// if
				// ((Math.abs(Math.abs(bFunc.getXi().getXi())-0.5)<0.00000001))
				// {
				N++;
				bounds.add(bFunc);
			}
		}
		// copy old elements;
		HashMatrixLUStyle A = new HashMatrixLUStyle(RowType.ARRAY_TYPE, mainMatrix.getN() + N);
		double[] newB = new double[mainMatrix.getN() + N];
		for (int i = 0; i < b.length; i++) {
			A.getRows().set(i, mainMatrix.getRows().get(i));
			newB[i] = b[i];
		}
		// calculate new data from lagrange influence
		int n = mainMatrix.getN();
		for (int i = 0; i < bounds.size(); i++) {
			for (int j = 0; j < b.length; j++) {
				// matrix influence
				Node nodeA = new Node();
				Node nodeB = new Node();
				nodeB.x = -0.5;
				nodeB.y = 0.5;
				nodeA.y = -0.5;
				nodeA.x = -0.5;
				double value = this.calcA(bounds.get(i), baseFunctions.get(j), nodeA, nodeB);
				nodeB.x = 0.5;
				nodeB.y = -0.5;
				nodeA.y = 0.5;
				nodeA.x = 0.5;
				value += this.calcA(bounds.get(i), baseFunctions.get(j), nodeA, nodeB);
				nodeB.x = -0.5;
				nodeB.y = 0.5;
				nodeA.y = 0.5;
				nodeA.x = 0.5;
				value += this.calcA(bounds.get(i), baseFunctions.get(j), nodeA, nodeB);
				nodeB.x = 0.5;
				nodeB.y = -0.5;
				nodeA.y = -0.5;
				nodeA.x = -0.5;
				value += this.calcA(bounds.get(i), baseFunctions.get(j), nodeA, nodeB);
				A.setValue(n + i, j, value);
				A.setValue(j, n + i, value);
			}
			// vector influence
			Node nodeA = new Node();
			Node nodeB = new Node();
			nodeB.x = -0.5;
			nodeB.y = 0.5;
			nodeA.y = -0.5;
			nodeA.x = -0.5;
			double value = this.calcB(bounds.get(i), 0.0, nodeA, nodeB);
			nodeB.x = 0.5;
			nodeB.y = -0.5;
			nodeA.y = 0.5;
			nodeA.x = 0.5;
			value += this.calcB(bounds.get(i), 0.0, nodeA, nodeB);
			nodeB.x = -0.5;
			nodeB.y = 0.5;
			nodeA.y = 0.5;
			nodeA.x = 0.5;
			value += this.calcB(bounds.get(i), 0.0, nodeA, nodeB);
			nodeB.x = 0.5;
			nodeB.y = -0.5;
			nodeA.y = -0.5;
			nodeA.x = -0.5;
			value += this.calcB(bounds.get(i), 0.0, nodeA, nodeB);
			newB[n + i] = value;
		}

		LUFactorization lu = new LUFactorization(A);

		return new EFGResult2D(lu.calculate(newB), baseFunctions);
	}

	public EFGResult2D calculatePenalty() {
		for (int i = 0; i < b.length; i++) {
			for (int j = 0; j < b.length; j++) {
				System.out.print(mainMatrix.getValue(i, j) + "\t");
			}
			System.out.println();
		}
		for (int j = 0; j < b.length; j++) {
			System.out.println(b[j]);
		}
		double MaxC = 100000000000.0;
		// Gauss2D gau = new Gauss2D(100);

		final double dirihletVal = 0.0;

		Node nodeA = new Node();
		Node nodeB = new Node();

		nodeB.x = -0.5;
		nodeB.y = 0.5;
		nodeA.y = -0.5;
		nodeA.x = -0.5;
		for (int i = 0; i < b.length; i++) {
			final BaseFunction f1 = baseFunctions.get(i);
			for (int j = 0; j < b.length; j++) {
				final BaseFunction f2 = baseFunctions.get(j);
				double val = gau.gaussOnGamma(nodeA, nodeB, new IFunction2D() {

					public double calculate(double x, double y) {
						return f1.calculate(x, y) * f2.calculate(x, y);
					}

					public double calculateDerivatyX(double x, double y) {
						throw new UnsupportedOperationException("Not supported yet.");
					}

					public double calculateDerivatyY(double x, double y) {
						throw new UnsupportedOperationException("Not supported yet.");
					}
				});
				mainMatrix.setValue(i, j, mainMatrix.getValue(i, j) + MaxC * (val));
			}
			b[i] += MaxC * gau.gaussOnGamma(nodeA, nodeB, new IFunction2D() {

				@Override
				public double calculate(double x, double y) {
					return f1.calculate(x, y) * dirihletVal;
				}

				@Override
				public double calculateDerivatyX(double x, double y) {
					return 0;
				}

				@Override
				public double calculateDerivatyY(double x, double y) {
					return 0;
				}
			});
		}

		nodeB.x = -0.5;
		nodeB.y = 0.5;
		nodeA.y = 0.5;
		nodeA.x = 0.5;
		for (int i = 0; i < b.length; i++) {
			final BaseFunction f1 = baseFunctions.get(i);
			for (int j = 0; j < b.length; j++) {
				final BaseFunction f2 = baseFunctions.get(j);
				double val = gau.gaussOnGamma(nodeA, nodeB, new IFunction2D() {

					public double calculate(double x, double y) {
						return f1.calculate(x, y) * f2.calculate(x, y);
					}

					public double calculateDerivatyX(double x, double y) {
						throw new UnsupportedOperationException("Not supported yet.");
					}

					public double calculateDerivatyY(double x, double y) {
						throw new UnsupportedOperationException("Not supported yet.");
					}
				});
				mainMatrix.setValue(i, j, mainMatrix.getValue(i, j) + MaxC * (val));
			}
			b[i] += MaxC * gau.gaussOnGamma(nodeA, nodeB, new IFunction2D() {

				@Override
				public double calculate(double x, double y) {
					return f1.calculate(x, y) * dirihletVal;
				}

				@Override
				public double calculateDerivatyX(double x, double y) {
					return 0;
				}

				@Override
				public double calculateDerivatyY(double x, double y) {
					return 0;
				}
			});

		}

		nodeB.x = 0.5;
		nodeB.y = -0.5;
		nodeA.y = 0.5;
		nodeA.x = 0.5;
		for (int i = 0; i < b.length; i++) {
			final BaseFunction f1 = baseFunctions.get(i);
			for (int j = 0; j < b.length; j++) {
				final BaseFunction f2 = baseFunctions.get(j);
				double val = gau.gaussOnGamma(nodeA, nodeB, new IFunction2D() {

					public double calculate(double x, double y) {
						return f1.calculate(x, y) * f2.calculate(x, y);
					}

					public double calculateDerivatyX(double x, double y) {
						throw new UnsupportedOperationException("Not supported yet.");
					}

					public double calculateDerivatyY(double x, double y) {
						throw new UnsupportedOperationException("Not supported yet.");
					}
				});
				mainMatrix.setValue(i, j, mainMatrix.getValue(i, j) + MaxC * (val));
			}
			b[i] += MaxC * gau.gaussOnGamma(nodeA, nodeB, new IFunction2D() {

				@Override
				public double calculate(double x, double y) {
					return f1.calculate(x, y) * dirihletVal;
				}

				@Override
				public double calculateDerivatyX(double x, double y) {
					return 0;
				}

				@Override
				public double calculateDerivatyY(double x, double y) {
					return 0;
				}
			});

		}

		nodeB.x = 0.5;
		nodeB.y = -0.5;
		nodeA.y = -0.5;
		nodeA.x = -0.5;
		for (int i = 0; i < b.length; i++) {
			final BaseFunction f1 = baseFunctions.get(i);
			for (int j = 0; j < b.length; j++) {
				final BaseFunction f2 = baseFunctions.get(j);
				double val = gau.gaussOnGamma(nodeA, nodeB, new IFunction2D() {

					public double calculate(double x, double y) {
						return f1.calculate(x, y) * f2.calculate(x, y);
					}

					public double calculateDerivatyX(double x, double y) {
						throw new UnsupportedOperationException("Not supported yet.");
					}

					public double calculateDerivatyY(double x, double y) {
						throw new UnsupportedOperationException("Not supported yet.");
					}
				});
				mainMatrix.setValue(i, j, mainMatrix.getValue(i, j) + MaxC * (val));
			}
			b[i] += MaxC * gau.gaussOnGamma(nodeA, nodeB, new IFunction2D() {

				@Override
				public double calculate(double x, double y) {
					return f1.calculate(x, y) * dirihletVal;
				}

				@Override
				public double calculateDerivatyX(double x, double y) {
					return 0;
				}

				@Override
				public double calculateDerivatyY(double x, double y) {
					return 0;
				}
			});

		}

		LUFactorization lu = new LUFactorization(mainMatrix);

		return new EFGResult2D(lu.calculate(b), baseFunctions);
	}

	public void generateMatrix() {
		mainMatrix = new HashMatrixLUStyle(RowType.ARRAY_TYPE, particles.size());
		b = new double[particles.size()];
		final EquationParameters eqp = this.parameters;
		for (int i = 0; i < b.length; i++) {
			final BaseFunction phi1 = this.baseFunctions.get(i);
			System.err.println(i);
			for (int j = 0; j < b.length; j++) {
				final BaseFunction phi2 = this.baseFunctions.get(j);
				System.err.println(j);
				if (intersects(phi1, phi2)) {
					continue;
				}
				double tmp = 0.0;
				// for (Iterator<TriangleL> iterator = triangleList.iterator();
				// iterator.hasNext();) {
				// TriangleL triangle = iterator.next();
				// tmp+=gau.gaussOnTriangle(new IFunction2D() {
				//
				// @Override
				// public double calculateDerivatyY(double x, double y) {
				//
				// return 0;
				// }
				//
				// @Override
				// public double calculateDerivatyX(double x, double y) {
				//
				// return 0;
				// }
				//
				// @Override
				// public double calculate(double x, double y) {
				// double val1 = phi1.calculate(x, y);
				// double val1x = phi1.calculate(x, y);
				// double val1y = phi1.calculate(x, y);
				// double val2 = phi2.calculate(x, y);
				// double val2x = phi2.calculate(x, y);
				// double val2y = phi2.calculate(x, y);
				// return val1x*val2x*eqp.Kxx(x, y) + val1y*val2x*eqp.Kyx(x, y)
				// + val1y*val2y*eqp.Kyy(x, y) + val1x*val2y*eqp.Kxy(x, y) +
				// val1x*val2*eqp.Px(x, y) + val1y*val2*eqp.Py(x, y) +
				// val1*val2*eqp.Q(x, y);
				// }
				// }, nodeList.get(triangle.A()), nodeList.get(triangle.B()),
				// nodeList.get(triangle.C()));
				// }

				tmp = gau.integrate(-0.5, 0.5, -0.5, 0.5, new IFunction2D() {

					@Override
					public double calculate(double x, double y) {
						double val1 = phi1.calculate(x, y);
						double val1x = phi1.calculateDerivatyX(x, y);
						double val1y = phi1.calculateDerivatyY(x, y);
						double val2 = phi2.calculate(x, y);
						double val2x = phi2.calculateDerivatyX(x, y);
						double val2y = phi2.calculateDerivatyY(x, y);
						return val1x * val2x * eqp.Kxx(x, y) + val1y * val2x * eqp.Kyx(x, y) + val1y * val2y * eqp.Kyy(x, y) + val1x * val2y * eqp.Kxy(x, y) + val1x * val2
								* eqp.Px(x, y) + val1y * val2 * eqp.Py(x, y) + val1 * val2 * eqp.Q(x, y);
					}

					@Override
					public double calculateDerivatyX(double x, double y) {

						return 0;
					}

					@Override
					public double calculateDerivatyY(double x, double y) {

						return 0;
					}
				});
				mainMatrix.setValue(i, j, mainMatrix.getValue(i, j) + tmp);
			}
			double tmp = 0.0;
			// for (Iterator<TriangleL> iterator = triangleList.iterator();
			// iterator.hasNext();) {
			// TriangleL triangle = iterator.next();
			// tmp+=gau.gaussOnTriangle(new IFunction2D() {
			//
			// @Override
			// public double calculateDerivatyY(double x, double y) {
			//
			// return 0;
			// }
			//
			// @Override
			// public double calculateDerivatyX(double x, double y) {
			//
			// return 0;
			// }
			//
			// @Override
			// public double calculate(double x, double y) {
			// double val1 = phi1.calculate(x, y);
			//
			// return val1*eqp.F(x, y);
			// }
			// }, nodeList.get(triangle.A()), nodeList.get(triangle.B()),
			// nodeList.get(triangle.C()));
			// }
			tmp = gau.integrate(-0.5, 0.5, -0.5, 0.5, new IFunction2D() {
				@Override
				public double calculate(double x, double y) {
					double val1 = phi1.calculate(x, y);
					return val1 * eqp.F(x, y);
				}

				@Override
				public double calculateDerivatyX(double x, double y) {

					return 0;
				}

				@Override
				public double calculateDerivatyY(double x, double y) {

					return 0;
				}
			});

			b[i] = tmp;
		}

		// for (int i = 0; i < b.length; i++) {
		// for (int j = 0; j < b.length; j++) {
		// System.out.print(mainMatrix.getValue(i, j)+"\t");
		// }
		// System.out.println();
		// }
		// for (int j = 0; j < b.length; j++) {
		// System.out.println(b[j]);
		// }

	}

	private double calcA(final BaseFunction bf1, final BaseFunction bf2, Node A, Node B) {
		double val = gau.gaussOnGamma(A, B, new IFunction2D() {

			public double calculate(double x, double y) {
				return bf1.calculate(x, y) * bf2.calculate(x, y);
			}

			public double calculateDerivatyX(double x, double y) {
				throw new UnsupportedOperationException("Not supported yet.");
			}

			public double calculateDerivatyY(double x, double y) {
				throw new UnsupportedOperationException("Not supported yet.");
			}
		});

		return val;
	}

	private double calcB(final BaseFunction bf1, final double value, Node A, Node B) {
		double val = gau.gaussOnGamma(A, B, new IFunction2D() {

			public double calculate(double x, double y) {
				return bf1.calculate(x, y) * value;
			}

			public double calculateDerivatyX(double x, double y) {
				throw new UnsupportedOperationException("Not supported yet.");
			}

			public double calculateDerivatyY(double x, double y) {
				throw new UnsupportedOperationException("Not supported yet.");
			}
		});

		return val;
	}

	private void createBaseFunctions(int PolinomialDegree, IWeightFunction2D weight) {
		this.baseFunctions = new ArrayList<BaseFunction>();
		for (Iterator<Particle> iterator = particles.iterator(); iterator.hasNext();) {
			Particle particle = iterator.next();
			BaseFunction bf = new BaseFunction(PolinomialDegree, particle, weight);
			bf.setNodeList(particles);
			this.baseFunctions.add(bf);
		}
	}

	private void readData(String folderDir) {
		triangleList = new ArrayList<TriangleL>();
		nodeList = new ArrayList<Node>();
		edges = new ArrayList<NodePair>();

		// read triangles;
		Scanner in = null;
		try {
			in = new Scanner(new FileInputStream(folderDir + "/file.1.ele"));
			int n = in.nextInt();
			in.nextInt();
			in.nextInt();
			for (int i = 0; i < n; i++) {
				in.next();
				triangleList.add(new TriangleL(Integer.parseInt(in.next()) - 1, Integer.parseInt(in.next()) - 1, Integer.parseInt(in.next()) - 1));
			}
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} finally {
			in.close();
		}
		// read nodes;
		try {
			in = new Scanner((new FileInputStream(folderDir + "/file.1.node")));
			int n = in.nextInt();
			in.nextInt();
			in.nextInt();
			in.nextInt();
			for (int i = 0; i < n; i++) {
				in.next();
				nodeList.add(new Node((Double.parseDouble(in.next())), (Double.parseDouble(in.next())), (isTrue(Integer.parseInt(in.next()))), i));
				// System.out.println(NodeList.get(i).x+"\t"+NodeList.get(i).y);
			}
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} finally {
			in.close();
		}
		// read edges;
		try {
			in = new Scanner((new FileInputStream(folderDir + "/file.1.poly")));
			in.nextInt();
			in.nextInt();
			in.nextInt();
			in.nextInt();
			int n = in.nextInt();
			in.nextInt();
			for (int i = 0; i < n; i++) {
				in.nextInt();
				int pos1 = in.nextInt() - 1, pos2 = in.nextInt() - 1;
				edges.add(new NodePair(nodeList.get(pos1), nodeList.get(pos2)));
				in.nextInt();
			}
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} finally {
			in.close();
		}

	}

	protected boolean intersects(BaseFunction f1, BaseFunction f2) {
		double Eps = 1E-7;
		Particle p1 = f1.getXi();
		Particle p2 = f2.getXi();
		boolean key = (Math.sqrt(p1.lengthTo(p2)) - (p1.getRho() + p2.getRho()) * Math.sqrt(2)) < Eps;

		if (!key) {
			Logger.getAnonymousLogger().log(Level.WARNING, "skipped");
		}
		return !key;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String FilePath = "C:/Users/marko/Documents/workspace/MyFEM/kvadrat/eps0";
		ArrayList<Particle> nodes = new ArrayList<Particle>();
		// параметри області:
		double ax = -0.5, bx = 0.5, ay = -0.5, by = 0.5;

		int n = 5, m = 5;

		double hx = (bx - ax) / (double) n, hy = (by - ay) / (double) m;

		// вузли:
		double rho = 1.5 * hx;
		for (int i = 0; i <= n; i++) {
			for (int j = 0; j <= m; j++) {
				Particle p = new Particle(ax + i * hx, ay + j * hy, rho);
				nodes.add(p);
			}
		}

		// equation parameters:
		EquationParameters params = new EquationParameters();
		params.setF(new ConstantFunction2D(10));
		params.setKxx(new ConstantFunction2D(1));
		params.setKyy(new ConstantFunction2D(1));
		params.setKxy(new ConstantFunction2D(1));
		params.setKyx(new ConstantFunction2D(1));
		params.setPx(new ConstantFunction2D(0));
		params.setPy(new ConstantFunction2D(0));
		params.setQ(new ConstantFunction2D(0));

		Solver2 solver = new Solver2(50, nodes, params, FilePath, 1, new SquareSupport(new CubicSpline()));

		solver.mainMatrix = readMatrix("C:/Users/marko/Desktop/a.poly", nodes.size());
		solver.b = readVec("C:/Users/marko/Desktop/b.poly", nodes.size());
		// solver.generateMatrix();
		// solver.calculatePenalty().display(new Range(-0.5, 0.5), new
		// Range(-0.5, 0.5));
		solver.calculate().display(new Range(-0.5, 0.5), new Range(-0.5, 0.5));
		// solver.calculateLagrange().display(new Range(-0.5, 0.5), new
		// Range(-0.5, 0.5));

	}

	public static HashMatrixLUStyle readMatrix(String file, int n) {
		// int n = 64;
		Scanner s = null;
		try {
			s = new Scanner(new FileInputStream(file));
			HashMatrixLUStyle a = new HashMatrixLUStyle(RowType.ARRAY_TYPE, n);
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					// s.hasNext();
					a.setValue(i, j, new Double(s.next()));
				}
			}
			return a;
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} finally {
			s.close();
		}
		return null;

	}

	public static double[] readVec(String file, int n) {
		// int n = 64;
		Scanner s = null;
		try {
			s = new Scanner(new FileInputStream(file));
			double[] b = new double[n];
			for (int i = 0; i < n; i++) {
				b[i] = new Double(s.next());
			}
			return b;
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} finally {
			s.close();
		}
		return null;

	}

	private static boolean isTrue(int s) {
		if (s == 0) {
			return false;
		} else {
			return true;
		}
	}

}
